<?php
/**
 * 水票管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Goods;

use App\Admin\Actions\Goods\WaterTicketBatchOffShelf;
use App\Admin\Actions\Goods\WaterTicketBatchOnShelf;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\GoodsWarehouseModel;
use App\Models\yb\WaterTicketModel;
use App\Models\yb\WaterTicketOrderModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;


class WaterTicketController extends Controller
{
    use ModelForm;

    protected $header = '水票管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    public function by_record(Request $request)
    {
        $id = $request->get('id');

        return Admin::content(function (Content $content)use($id) {

            $content->header($this->header);
            $content->description('购买明细');
            $content->body($this->by_record_grid($id));

        });
    }

    public function grid(){

        $grid = new Grid(new WaterTicketModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','水票编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('water_ticket_name','水票标题')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_type_id','类型')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : GoodsTypeModel::selectValue('id',$filed);
        });

        $grid->column('goods_name','商品名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_id','商品规格')->display(function($filed){
            if(empty($filed)){
                $data = empty($filed);
            }else{
                $goods_data = GoodsWarehouseModel::where(['id'=>$filed])->select('goods_specs')->first();
                $data = $goods_data['goods_specs'];
            }
            return $data;
        });

        $grid->column('water_ticket_price','单价')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('promotion_by_give','促销')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('equivalent_unit_price','折合单价')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('amount_price','总价')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('status','状态')->using(DataDictionaryModel::Status, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        $grid->column('action_id','操作')->display(function($filed){
            if($this->status == 1){
                return '<a href="by/record?id='.$filed.'" class="grid-row-edit">
                        <button type="button" class="btn btn-info btn-xs btn-flat">购买记录</button>
                    </a>';
            }else{
                return '<a href="manage/'.$filed.'/edit" class="grid-row-edit">
                        <button type="button" class="btn btn-primary btn-xs btn-flat">编辑</button>
                    </a>
                    <a href="by/record?id='.$filed.'" class="grid-row-edit">
                        <button type="button" class="btn btn-info btn-xs btn-flat">购买记录</button>
                    </a>';
            }
        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('water_ticket_name','搜索水票');
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
                $filter->between('created_at', '创建时间')->datetime();
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('goods_name','商品名称');
                $filter->equal('goods_type', '类型')->select(GoodsTypeModel::selectOptions());
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->disableActions();

        $grid->tools(function ($tools) {
            $tools->append(new WaterTicketBatchOnShelf());
            $tools->append(new WaterTicketBatchOffShelf());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new WaterTicketModel());

        $form->text('water_ticket_name', '水票名称')->rules('required|max:150');

        $form->select('goods_type_id', '类型')->options(GoodsTypeModel::selectOptions())->rules('required');

        $form->select('goods_id', '商品')->options(GoodsWarehouseModel::selectOptions())->rules('required');

        $form->currency('water_ticket_price', '单价')->symbol('￥')->rules('required');

        $form->html(view('admin.water_ticket.promotion'),'促销');

        $form->currency('equivalent_unit_price', '折合单价')->symbol('￥')->readonly(0);

        $form->currency('amount_price', '总价')->symbol('￥')->readonly(0);

        $form->hidden('goods_name');


        Admin::script("
        $('#water_ticket_price').bind('input propertychange', function() {
            var water_ticket_price = parseFloat($('#water_ticket_price').val());  //单价
            var promotion_by = parseFloat($('#promotion_by').val());  //购买数量
            var promotion_give = parseFloat($('#promotion_give').val());  //赠送数量
            var amount_price = parseFloat(water_ticket_price*promotion_by);  //总价
            var equivalent_unit_price = amount_price/(promotion_by+promotion_give);  //折合单价
            var equivalent_unit_price = equivalent_unit_price.toFixed(2);
            $(\"#amount_price\").val(amount_price);
            $(\"#equivalent_unit_price\").val(equivalent_unit_price);
        });
        $('#promotion_by').bind('input propertychange', function() {
            var water_ticket_price = parseFloat($('#water_ticket_price').val());  //单价
            var promotion_by = parseFloat($('#promotion_by').val());  //购买数量
            var promotion_give = parseFloat($('#promotion_give').val());  //赠送数量
            var amount_price = parseFloat(water_ticket_price*promotion_by);  //总价
            var equivalent_unit_price = amount_price/(promotion_by+promotion_give);  //折合单价
            var equivalent_unit_price = equivalent_unit_price.toFixed(2);
            $(\"#amount_price\").val(amount_price);
            $(\"#equivalent_unit_price\").val(equivalent_unit_price);
        });
        $('#promotion_give').bind('input propertychange', function() {
            var water_ticket_price = parseFloat($('#water_ticket_price').val());  //单价
            var promotion_by = parseFloat($('#promotion_by').val());  //购买数量
            var promotion_give = parseFloat($('#promotion_give').val());  //赠送数量
            var amount_price = parseFloat(water_ticket_price*promotion_by);  //总价
            var equivalent_unit_price = amount_price/(promotion_by+promotion_give);  //折合单价
            var equivalent_unit_price = equivalent_unit_price.toFixed(2);
            $(\"#amount_price\").val(amount_price);
            $(\"#equivalent_unit_price\").val(equivalent_unit_price);
        });
        ");

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->saving(function (Form $form) {
            $goods_id = $form->goods_id;
            $goods_title = GoodsWarehouseModel::selectValue('id',$goods_id);
            $form->goods_name = $goods_title;
        });


        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            WaterTicketModel::where(['id'=>$id])->update([
                'promotion_by'=>$form->promotion_by,
                'promotion_give'=>$form->promotion_give,

            ]);

        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }

    public function by_record_grid($id){

        $grid = new Grid(new WaterTicketOrderModel());

        $grid->model()->where(['status'=>1,'water_ticket_id'=>$id])->orderBy('created_at', 'DESC');

        $grid->column('order_sn','订单号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('user_id','用户ID')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('user_name','用户昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('user_phone','用户电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('water_ticket_name','水票名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('pay_time','支付时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('id','支付方式')->display(function($filed){
            return "微信";
        });

        $grid->column('amount','支付金额')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });


        $grid->column('created_at','操作时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('order_sn','订单号');
                $filter->like('user_name','用户昵称');
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('user_phone','用户电话');
                $filter->between('created_at', '创建时间')->datetime();
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->disableActions();

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;


    }

}
